package huxibianjie.com.lbscandroid.ui.new_fragment.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.model.AccountDetailsModel;


/**
 * 账户明细
 */

public class AccountDetailsAdapter extends RecyclerView.Adapter<AccountDetailsAdapter.BaseViewHolder> {
    private List<AccountDetailsModel> dataList = new ArrayList<>();
    private Activity activity;
    private String type;

    public AccountDetailsAdapter(Activity activity, String type) {
        this.activity = activity;
        this.type = type;
    }

    public void clearAll() {
        dataList.clear();
        notifyDataSetChanged();
    }

    public void replaceAll(@Nullable List<AccountDetailsModel> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addAll(@Nullable List<AccountDetailsModel> list) {
        if (list != null && list.size() > 0) {
            int start = this.dataList.size();
            this.dataList.addAll(list);
            notifyItemRangeInserted(start, list.size());
        }
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_account_details, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position));
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle, tvTime, tvMoney;

        BaseViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMoney = itemView.findViewById(R.id.tv_money);
        }

        private AccountDetailsModel model;

        public void setData(Object data) {
            model = (AccountDetailsModel) data;
            tvTitle.setText(type.equals("1") ? "上传位置获取" : "提现");
            tvTime.setText(String.valueOf(model.getCreateTime()));
            tvMoney.setText(String.valueOf((type.equals("1") ? "+" : "-") + model.getMoney()));
        }
    }
}
